package zhongbo.java.air.flightpath.web.controller;


import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.common.vo.ResultVo;
import zhongbo.java.air.flightpath.model.AirFlightPath;
import zhongbo.java.air.flightpath.service.impl.AirFlightPathServiceImpl;

import java.util.Date;
import java.util.List;

@RestController
@Api(tags = "航迹记录相关")
@RequestMapping("/api/airFlightPath")
public class FlightPathController {

    @Autowired
    private AirFlightPathServiceImpl airFlightPathService;

    @ApiOperation("新增航迹记录")
    @PostMapping("/addAirFlightPath")
    public ResultVo<String> addAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
        String add = airFlightPathService.addAirFlightPath(airFlightPath);
        if (StrUtil.isNotBlank(add)) {
            return ResultVo.ok(add);
        }
        return ResultVo.error("新增航迹记录失败");
    }
    @ApiOperation("修改航迹记录")
    @PutMapping("/updateAirFlightPath")
    public ResultVo<AirFlightPath> updateAirFlightPath(@RequestBody AirFlightPath airFlightPath) {
        AirFlightPath update = airFlightPathService.updateAirFlightPath(airFlightPath);
        if (update != null) {
            return ResultVo.ok(update);
        }
        return ResultVo.error("修改航迹记录失败");
    }

    @ApiOperation("查询一个航行轨迹")
    @GetMapping("/getAirFlightPath")
    public ResultVo<AirFlightPath> getAirFlightPath(@RequestParam(defaultValue = "1") String airId) {
        AirFlightPath airFlightPath = airFlightPathService.getAirFlightPathById(airId);
        return ResultVo.ok(airFlightPath);
    }

    @ApiOperation("删除一个航行轨迹")
    @DeleteMapping("/deleteAirFlightPath")
    public ResultVo<Integer> deleteAirFlightPath(@RequestParam(defaultValue = "1") String airId) {
        int delete = airFlightPathService.deleteAirFlightPath(airId);
        return ResultVo.ok(delete);
    }


//    @ApiOperation("获取所有航行轨迹")
//    @GetMapping("getAllAirFlightPath")
//    public ResultVo<List<AirFlightPath>> getAllAirFlightPath() {
//        List<AirFlightPath> allAirFlightPath = airFlightPathService.getAirFlightPathAll();
//        return ResultVo.ok(allAirFlightPath);
//    }

    @ApiOperation("分页获取所有航行轨迹")
    @GetMapping("pageAllAirFlightPath")
    public ResultVo<PageVo<AirFlightPath>> page(@RequestParam(required = false) String icaoCode,
                                                @RequestParam(defaultValue = "1") Integer pageNumber,
                                                @RequestParam(defaultValue = "1") Integer pageSize) {
        PageVo<AirFlightPath> page = airFlightPathService.page(icaoCode,pageNumber,pageSize);
        return ResultVo.ok(page);
    }

    @ApiOperation("分页获取所有航行轨迹(可以设置时间)")
    @GetMapping("/page")
    public ResultVo<PageVo<AirFlightPath>> pageFlightPathAll (@RequestParam(required = true) String icao,
                                                              @RequestParam(defaultValue = "2025-04-02 14:42:40.039") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") Date startTime,
                                                              @RequestParam(defaultValue = "2025-04-07 11:11:41.266") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS") Date endTime,
                                                              @RequestParam(defaultValue = "1") Integer pageNumber,
                                                              @RequestParam(defaultValue = "1") Integer pageSize) {
        PageVo<AirFlightPath> page = airFlightPathService.pageTimeFlightPathAll(icao, startTime, endTime, pageNumber, pageSize);
        return ResultVo.ok(page);
    }
}
